﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace ClientAppraise.Controls;
public class ZxUniformWrapPanel : WrapPanel
{
    public static readonly DependencyProperty ItemMinWidthProperty;
    public static readonly DependencyProperty ItemMaxWidthProperty;
    public static readonly DependencyProperty MaxDisplayRowProperty;
    public static readonly DependencyProperty MustDisplayIndexProperty;
    public static readonly DependencyProperty MinDisplayItemCountProperty;

    static ZxUniformWrapPanel()
    {
        ItemMinWidthProperty = DependencyProperty.Register(nameof(ItemMinWidth), typeof(double), typeof(ZxUniformWrapPanel));
        ItemMaxWidthProperty = DependencyProperty.Register(nameof(ItemMaxWidth), typeof(double), typeof(ZxUniformWrapPanel));
        MaxDisplayRowProperty = DependencyProperty.Register(nameof(MaxDisplayRow), typeof(int), typeof(ZxUniformWrapPanel), new FrameworkPropertyMetadata(999));
        MustDisplayIndexProperty = DependencyProperty.Register(nameof(MustDisplayIndex), typeof(int), typeof(ZxUniformWrapPanel));
        MinDisplayItemCountProperty = DependencyProperty.Register(nameof(MinDisplayItemCount), typeof(int), typeof(ZxUniformWrapPanel));

    }

    public double ItemMinWidth
    {
        get => (double)GetValue(ItemMinWidthProperty);
        set => SetValue(ItemMinWidthProperty, value);
    }
    public double ItemMaxWidth
    {
        get => (double)GetValue(ItemMaxWidthProperty);
        set => SetValue(ItemMaxWidthProperty, value);
    }
    public int MaxDisplayRow
    {
        get => (int)GetValue(MaxDisplayRowProperty);
        set => SetValue(MaxDisplayRowProperty, value);
    }
    public int MustDisplayIndex
    {
        get => (int)GetValue(MustDisplayIndexProperty);
        set => SetValue(MustDisplayIndexProperty, value);
    }
    public int MinDisplayItemCount
    {
        get => (int)GetValue(MinDisplayItemCountProperty);
        set => SetValue(MinDisplayItemCountProperty, value);
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        _ = UpdateChildSize(availableSize);

        return base.MeasureOverride(availableSize);
    }

    private Size UpdateChildSize(Size availableSize)
    {
        Size panelDesiredSize = new Size();

        double tempRowItemCount = 1;
        double tempRowCount = 1;


        //按最小宽度放入，最多可以放入的数量
        tempRowItemCount = double.Floor(availableSize.Width / (ItemMinWidth * 1.0f));

        if (tempRowItemCount >= this.Children.Count)
        {
            tempRowItemCount = this.Children.Count;
        }
        else
        {
            tempRowCount = double.Ceiling(this.Children.Count / (tempRowItemCount * 1.0f));

            if (tempRowCount > MaxDisplayRow)
            {
                tempRowCount = MaxDisplayRow;
            }
        }


        if (tempRowCount * tempRowItemCount < this.Children.Count)
        {
            //从后向前隐藏
            var count = tempRowCount * tempRowItemCount - this.Children.Count;
            int currentIndex = this.Children.Count - 1;

            while (count > 0)
            {
                if (currentIndex == MustDisplayIndex) continue;

                this.Children[currentIndex].Visibility = Visibility.Collapsed;

                currentIndex--;
                count--;

                if (currentIndex < 1) break;
            }
        }
        else if (tempRowCount * tempRowItemCount > this.Children.Count)
        {

            var count = this.Children.Count - tempRowCount * tempRowItemCount;
            int currentIndex = this.Children.Count - 1;

            if (MustDisplayIndex > 0)
            {
                currentIndex = MustDisplayIndex - 1;
            }

            while (count > 0)
            {
                if (currentIndex == MustDisplayIndex) continue;

                this.Children.Insert(currentIndex, new FrameworkElement());

                count--;
            }
        }

        this.ItemWidth = availableSize.Width / tempRowItemCount;

        panelDesiredSize.Width = availableSize.Width;
        panelDesiredSize.Height = this.ItemHeight * tempRowCount;

        return panelDesiredSize;
    }

}
